package top.kaoshanji.r2ch07c;

import top.kaoshanji.r2ch07b.PerformanceMonitor;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * 代码清单-r2ch07c-225页b：实现JDK动态代理的拦截器处理接口
 * @author kaoshanji
 * ^_^
 * create time 2025/5/7 14:56
 */
public class PerformanceHandler implements InvocationHandler {

    private Object target;

    public PerformanceHandler(Object target) { // target 为目标业务类
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        // 为目标业务类增强功能
        PerformanceMonitor.begin(target.getClass().getName() + "." + method.getName());

        // 通过反射方法调用业务类的目标方法
        Object obj = method.invoke(target, args);

        PerformanceMonitor.end();

        return obj;
    }
}
